package com.example.curriculum.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.curriculum.R;
import com.example.curriculum.RecordActivity;
import com.example.curriculum.bean.ClassBean;
import com.example.curriculum.bean.CurriculumBean;
import com.example.curriculum.fragment.PianoFragment.ViewHolder;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author DanielG
 * 
 */
public class ClassFragment extends Fragment implements OnItemClickListener, OnItemSelectedListener
{
	private static final String		TAG_CLASS	= "class";
	private ListView				mLvRegister;
	private ListView				mLvMenu;
	private List<ClassBean>			mClassDatas;			// 课程数量 = rb的数量
	private List<CurriculumBean>	mCurriculumDatas;		// 课程数量 = rb的数量
	private FragmentManager			fm;
	ClassBean						classBean	= null;

	private Handler					handler		= new Handler() {
													public void handleMessage(android.os.Message msg)
													{
														registerAdapter.notifyDataSetChanged();
													};
												};
	private RegisterListViewAdapter	registerAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		// TODO 填充布局

		View view = View.inflate(getActivity(), R.layout.fragment_class, null);
		mLvRegister = (ListView) view.findViewById(R.id.record_lv_containaer);
		mLvMenu = (ListView) view.findViewById(R.id.record_lv_curriculum);
		return view;

	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState)
	{
		// TODO 加载数据？
		super.onActivityCreated(savedInstanceState);
		// initFragment();
		initData();

	}

	private void initData()
	{
		// TODO 到网络 加载各种数据

		CurriculumBean curriculumBean = null;
		mClassDatas = new ArrayList<ClassBean>();
		 for (int i = 0; i < 7; i++)
		 {
		 classBean = new ClassBean();
		 mClassDatas.add(classBean);
		 }
		mCurriculumDatas = new ArrayList<CurriculumBean>();
		for (int i = 0; i < 10; i++)
		{
			curriculumBean = new CurriculumBean();
			curriculumBean.setName("素描" + i);
			mCurriculumDatas.add(curriculumBean);
		}
		mLvMenu.setAdapter(new MenuListViewAdapter());
		registerAdapter = new RegisterListViewAdapter();
		mLvRegister.setAdapter(registerAdapter);
//		Drawable selector = getActivity().getResources()
//                .getDrawable(R.drawable.item_menu_bg);
//		mLvMenu.setSelector(selector);
		mLvMenu.setOnItemClickListener(this);
		mLvMenu.setOnItemSelectedListener(this);
	}

	/**
	 * 右侧消费的记录 适配器
	 * 
	 * @author DanielG
	 * 
	 */
	class RegisterListViewAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return mClassDatas != null ? mClassDatas.size() : 0;
		}

		@Override
		public Object getItem(int position)
		{
			return mClassDatas != null ? mClassDatas.get(position) : null;
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = View.inflate(getActivity(), R.layout.item_class_hour, null);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();

			}
			return convertView;
		}

	}

	class ViewHolder
	{

	}

	/**
	 * 左侧菜单按键的适配器 各种课程
	 * 
	 * @author DanielG
	 * 
	 */
	class MenuListViewAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{

			return mCurriculumDatas != null ? mCurriculumDatas.size() : 0;
		}

		@Override
		public Object getItem(int position)
		{

			return mCurriculumDatas != null ? mCurriculumDatas.get(position) : null;

		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			if (convertView == null)
			{
				convertView = View.inflate(getActivity(), R.layout.item_curriculum_menu, null);
			}
			TextView tv = (TextView) convertView;
			// 给视图加载数据
			CurriculumBean bean = mCurriculumDatas.get(position);
			tv.setText(bean.getName());
			// 判断默认选中项
			// if (mCurrentItem == position)
			// {
			// // 让textView enable
			// tv.setEnabled(true);
			// } else {
			// // 让textView disable
			// tv.setEnabled(false);
			// }
			// tv.setEnabled(mCurrentItem == position);

			// 返回显示数据的View
			return convertView;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		// TODO 加载右侧的 数据
		view.setSelected(true);
		mClassDatas.clear();
		for (int i = 0; i < position; i++)
		{
			classBean = new ClassBean();
			mClassDatas.add(classBean);
		}
		// 发信息 刷新UI
		handler.sendEmptyMessage(0);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent)
	{
		// TODO Auto-generated method stub

	}

}
